<?php

namespace App\Controllers;

use App\Models\StudyLocationModel;
use App\Models\KeywordMappingModel;
use App\Models\PageLinksModel;
use App\Models\FeatureStatusModel;

class Page extends BaseController
{
    public function locationDetail($codeStr)
    {
        // 实例化 StudyLocationModel 模型
        $studyLocationModel = new StudyLocationModel();
        $keywordMappingModel = new KeywordMappingModel();
        $featureStatusModel = new FeatureStatusModel();

        $canEditLinks = $featureStatusModel->where('feature_key', "can_edit_links")->first();
        $editLinkStatus = $canEditLinks["status"];

        $canEditKeywords = $featureStatusModel->where('feature_key', "can_edit_keywords")->first();
        $editKeywordStatus = $canEditKeywords["status"];

        $canShowKeywords = $featureStatusModel->where('feature_key', "can_show_keywords")->first();
        $showKeywordStatus = $canShowKeywords["status"];

        // 使用 where 子句根据 locationId 查询 study_location 表中的数据
        $location = $studyLocationModel->where('code_str', $codeStr)->first();

        if (!$location) {
            return redirect()->to('/');
        }

        // 实例化 KeywordMappingModel 模型
        $keywordMappingModel = new KeywordMappingModel();
        // 根据 page_id 查询 keyword_mapping 表中的数据
        $keywordMappings = $keywordMappingModel->where('page_id', $location['id'])->findAll();

        $keywords = [];
        // 检查 $keywordMappings 是否非空
        if (!empty($keywordMappings)) {
            foreach ($keywordMappings as $mapping) {
                // 将每个 mapping 中的 keyword 字段值添加到 keywords 数组中
                if (isset($mapping['keyword'])) {
                    $keywords[] = $mapping['keyword'];
                }
            }
        }

        // 实例化 PageLinksModel 模型
        $pageLinksModel = new PageLinksModel();
        // 根据 page_id 查询 keyword_mapping 表中的数据
        $pageLinks = $pageLinksModel->where('from_page_id', $location['id'])->findAll();

        $links = [];
        $linkData = [];
        // 检查 $keywordMappings 是否非空
        if (!empty($pageLinks)) {
            foreach ($pageLinks as $link) {
                // 将每个 mapping 中的 keyword 字段值添加到 keywords 数组中
                if (isset($link['link_url'])) {
                    $tLocation = $studyLocationModel->find($link['to_page_id']);
                    $linkData[] = [
                        'link_url' => $link['link_url'],
                        'link_txt' => $link['link_txt'],
                        'locationOrder' => $tLocation['location_order'],
                        'from_page_id' => $link['from_page_id'],
                        'to_page_id' => $link['to_page_id'],
                    ];
                    $links[] = $link['link_url'];
                }
            }
        }
        // dd($links);
        $allLinks = [];
        $allLocation = $studyLocationModel->where("is_open", 1)->orderBy("location_order", "ASC")->findAll();
        foreach ($allLocation as $tLocation) {
            if ($tLocation['intro_url'] != $location['intro_url'] && !in_array($tLocation['intro_url'], $links)) {
                if (isset($tLocation['intro_url']) && isset($tLocation['name'])) {
                    $allLinks[] = [
                        'linkTxt' => $tLocation['name'],
                        'locationOrder' => $tLocation['location_order'],
                        'introUrl' => $tLocation['intro_url'],
                        'toPageId' => $tLocation['id']
                    ];
                }
            }
            
        }
        $data = [
            'title' => $location['name'] . ' - 研学介绍',
            'location' => $location,
            'keywords' => $keywords,
            'links' => $links,
            'editLinkStatus' => $editLinkStatus,
            'editKeywordStatus' => $editKeywordStatus,
            'showKeywordStatus' => $showKeywordStatus,
            'linkData' => $linkData,
            'allLinks' => $allLinks,
            'mainContent' => view('yanxue/' . $codeStr, ["location" => $location])
        ];
        
        return view('layouts/yanxue', $data);
    }
}